package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.server.api.BlackListApi;
import com.tanhua.dubbo.server.api.QuestionApi;
import com.tanhua.dubbo.server.api.SettingApi;
import com.tanhua.server.interceptor.UserThreadLocal;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Service
public class SettingService {

    @Reference
    private SettingApi settingApi;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private BlackListApi blackListApi;

    /**
     * 查询配置
     *
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity<SettingsVo> querySettings() {

        User user = UserThreadLocal.get();

        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setPhone(user.getMobile());
        settingsVo.setId(user.getId());

        // 查询通用的设置
        Settings settings = settingApi.findUserId(UserThreadLocal.getUserId());

        if (null != settings) {
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
        }

        //查询陌生人问题
        Question question = this.questionApi.queryQuestion(user.getId());

        if (null != question) {
            settingsVo.setStrangerQuestion(question.getTxt());
        }

        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置陌生人问题
     */
    @PostMapping("questions")
    public ResponseEntity<Void> saveQuestions(String content) {
        //判断问题是否存在，如果存在，进行修改，如果不存在，就新增操作
        Long userId = UserThreadLocal.getUserId();
        Question question = this.questionApi.queryQuestion(userId);

        if(null != question){
            question.setTxt(content);
            this.questionApi.update(question);
        }else{
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            this.questionApi.save(question);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 查询黑名单
     */
    public ResponseEntity<PageResult> queryBlacklist(Integer page, Integer pagesize) {
        IPage<UserInfo> iPage = this.blackListApi.queryPageList(UserThreadLocal.getUserId(), page, pagesize);
        PageResult pageResult = new PageResult();
        pageResult.setPagesize(pagesize);
        pageResult.setPage(page);
        pageResult.setCounts(new Long(iPage.getTotal()).intValue());
        pageResult.setPages(new Long(iPage.getPages()).intValue());
        pageResult.setItems(iPage.getRecords());
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 移除黑名单
     */
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity<Void> delBlacklist(Long userId,long delUserId) {
        blackListApi.delBlacklist(userId,delUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新通知设置
     */
    @PostMapping("notifications/setting")
    public ResponseEntity<Void> updateNotification(boolean likeNotification,boolean pinglunNotification,boolean gonggaoNotification) {
        Settings settings = settingApi.findUserId(UserThreadLocal.getUserId());
        if(settings != null) {
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingApi.update(settings);
        }else {
            settings = new Settings();
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setUserId(UserThreadLocal.getUserId());
            settingApi.save(settings);
        }
        return ResponseEntity.ok(null);
    }

}
